import 'package:baselib/utils/log_util.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
/// 路由跳转工具类
class NavigatorUtil {
  static pushByName(
      String page, {
        dynamic arguments,
        int? id,
        bool preventDuplicates = true,
        Map<String, String>? parameters,
        Function(Object)? resultFunc,
      }) {
    Get.toNamed(page,
        arguments: arguments,
        id: id,
        preventDuplicates: preventDuplicates,
        parameters: parameters,
    )
        ?.then((result) {
      // 页面返回result为null
      if (result == null) {
        return;
      }
      if (resultFunc != null) resultFunc(result);
    }).catchError((error) {
      LogUtil.d("$error");
    });
  }

  static offAllByName(
      String newRouteName, {
        RoutePredicate? predicate,
        dynamic arguments,
        int? id,
        Map<String, String>? parameters,
        Function(Object)? resultFunc,
      }) {
    Get.offAllNamed(newRouteName,
        arguments: arguments,
        id: id,
        predicate: predicate,
        parameters: parameters
    )
        ?.then((result) {
      // 页面返回result为null
      if (result == null) {
        return;
      }
      if (resultFunc != null) resultFunc(result);
    }).catchError((error) {
      LogUtil.d("$error");
    });
  }
  static offNamed(
      String newRouteName, {
        bool? preventDuplicates,
        dynamic arguments,
        int? id,
        Map<String, String>? parameters,
        Function(Object)? resultFunc,
      }) {
    Get.offNamed(newRouteName,
        arguments: arguments,
        id: id,
        preventDuplicates: preventDuplicates??true,
        parameters: parameters)
        ?.then((result) {
      // 页面返回result为null
      if (result == null) {
        return;
      }
      if (resultFunc != null) resultFunc(result);
    }).catchError((error) {
      LogUtil.d("$error");
    });
  }

  /// 返回
  static void goBack({
    result,
  }) {
    if (result != null) {
      Get.back(result: result);
    } else {
      Get.back();
    }
  }
}